package com.cloud.xtools.module.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.xtools.api.user.client.UserClient;
import com.cloud.xtools.api.user.domain.resource.UserResource;
import com.cloud.xtools.common.response.PageList;
import com.cloud.xtools.common.util.XPage;
import com.cloud.xtools.module.domain.entity.ModuleCollection;
import com.cloud.xtools.module.domain.query.admin.ModuleCollectionQuery;
import com.cloud.xtools.module.domain.resource.admin.ModuleCollectionResource;
import com.cloud.xtools.module.mapper.ModuleCollectionMapper;
import com.cloud.xtools.module.service.admin.ModuleCollectionService;
import com.cloud.xtools.module.service.admin.ModuleService;
import com.github.pagehelper.PageInfo;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ModuleCollectionServiceImpl extends ServiceImpl<ModuleCollectionMapper, ModuleCollection> implements ModuleCollectionService {

    private final HttpServletRequest request;
    private final UserClient userClient;

    private final ModuleService moduleService;

    @Override
    public PageList collectionList(ModuleCollectionQuery moduleCollectionQuery) {
        XPage.startPage(moduleCollectionQuery.getPageNum(), moduleCollectionQuery.getPageSize());

        PageInfo<ModuleCollection> pageInfo = new PageInfo<>(list());

        List<ModuleCollectionResource> moduleCollections = BeanUtil.copyToList(pageInfo.getList(), ModuleCollectionResource.class);

        moduleCollections.forEach(moduleCollectionResource -> {
            // 模块信息
            moduleCollectionResource.setModule(moduleService.getModuleById(moduleCollectionResource.getModuleId()));
            // 用户信息
            UserResource user = userClient.userInfoById(moduleCollectionResource.getUserId()).getData();
            moduleCollectionResource.setUser(user);
        });

        // Dump.json(moduleCollections);

        return new PageList(
                pageInfo.getPageNum(),
                pageInfo.getPageSize(),
                pageInfo.getTotal(),
                pageInfo.getPages(),
                moduleCollections
        );
    }
}
